<?php $max = (int)ini_get('upload_max_filesize');?>
<?php if(!isset($fromDb)) $fromDb=false?>
<script type="text/javascript">
function addFiles(varResult)
{	
	var fromDb = <?php echo $fromDb? 1 : 0; ?>;
	params = '<?php echo $fromDb?'owner_id=' . $self->id . '&ownerclass=' . get_class($self):'';?>';
	
	var xhrFiles = getXmlHttpRequest2();
	xhrFiles.onreadystatechange = function()
	{
		if(xhrFiles.readyState == 4 && xhrFiles.status == 200)
		{
			var tableauFichiers = xhrFiles.responseText;
			if(varResult>0){
				switch (varResult)
				{
					case 1:
						 // UPLOAD_ERR_INI_SIZE      
	                	varErrMsg =  "<?php echo __('Maximum file size is '.$max.' Mbytes.').'<br />'?>";   
	                break;
	   				case 2: // UPLOAD_ERR_FORM_SIZE   
						varErrMsg =  "<?php echo __('Maximum file size is '.$max.' Mbytes.').'<br />'?>"; 
					break;
					case 3: // UPLOAD_ERR_PARTIAL   
						varErrMsg =  "<?php echo __('An error has ocurred while sending file to server.').'<br />'?>";
					break;
					case 4: // UPLOAD_ERR_NO_FILE   
						varErrMsg =  "<?php echo __('You have sent an empty file.').'<br />'?>";
					break;
					default: 
						varErrMsg =  "<?php echo __('An error has ocurred while sending file to server.').'<br />'?>";
					break;
				}
				varErrDiv = '<div class="formHint" style="color: rgb(255, 0, 0);">'+varErrMsg+'</div>' ;   
				tableauFichiers = varErrDiv + tableauFichiers;
	        }  
			document.getElementById('listeFichiers').innerHTML = tableauFichiers;
		}
	};

	if(fromDb)
	{
		xhrFiles.open("POST","<?php print url_for('attachments/renderFilesFromDb'); ?>",true);
	}
	else
	{
		xhrFiles.open("POST","<?php print url_for('attachments/renderFiles'); ?>",true);
	}
	xhrFiles.setRequestHeader('X_REQUESTED_WITH','XMLHttpRequest');
	xhrFiles.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhrFiles.send(params);
}


function removeFiles(idFile)
{
	var xhrFiles = getXmlHttpRequest2();
	xhrFiles.onreadystatechange = function()
	{
		if(xhrFiles.readyState == 4 && xhrFiles.status == 200)
		{
			tableauClients = xhrFiles.responseText;
			document.getElementById('listeFichiers').innerHTML = tableauClients;
		}
	};
	xhrFiles.open("POST","<?php print url_for('attachments/removeFiles'); ?>",true);
	xhrFiles.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhrFiles.send("id="+idFile);
}
</script>

<script type="text/javascript">
$(document).ready(function(){    
	 addFiles(0);/*Show files without error*/
});

</script>
